package org.aceor.mddal.atom.factory;

import com.google.common.base.Strings;
import org.aceor.mddal.config.datasource.Atom;
import org.apache.commons.dbcp.BasicDataSource;

/**
 * Created by lxue on 16/3/29.
 */
public class DsFactory {
    public static BasicDataSource buildDbcpDatasource(Atom atom) {
        BasicDataSource ds = new BasicDataSource();
        if (!Strings.isNullOrEmpty(atom.getPassword())) {
            ds.setPassword(atom.getPassword());
        }
        if (!Strings.isNullOrEmpty(atom.getUrl())) {
            //去空格.因为使用了 <![CDATA[]>,会使得url前后都有'\n'
            ds.setUrl(atom.getUrl().trim());
        }
        if (!Strings.isNullOrEmpty(atom.getUsername())) {
            ds.setUsername(atom.getUsername());
        }
        if (!Strings.isNullOrEmpty(atom.getDriverClassName())) {
            ds.setDriverClassName(atom.getDriverClassName());
        }
        if (atom.getMaxActive() > 0) {
            ds.setMaxActive(atom.getMaxActive());
        }
        if (atom.getMaxIdle() > 0) {
            ds.setMaxIdle(atom.getMaxIdle());
        }
        if (atom.getMinIdle() > 0) {
            ds.setMinIdle(atom.getMinIdle());
        }
        if (atom.getMaxWait() > 0) {
            ds.setMaxWait(atom.getMaxWait());
        }
        if (atom.getInitialSize() > 0) {
            ds.setInitialSize(atom.getInitialSize());
        }
        return ds;
    }
}
